package com.recruit.entity.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.recruit.entity.Recruit;


public class RecruitQueryDTO {
	private Long id;
	private String title;//招聘信息标题
	private String position;//岗位
	private String requirements;//岗位要求
	private String description;//岗位描述
	private String number;//人数
	private String place;//工作地点
	private String salary;//薪资待遇
	private String mailbox;//投递简历邮箱
	private String introduction;//公司简介
	private Date publishDate;//发布日期

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getPublishDate() {
		return publishDate;
	}

	public void setPublishDate(Date publishDate) {
		this.publishDate = publishDate;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	public String getRequirements() {
		return requirements;
	}

	public void setRequirements(String requirements) {
		this.requirements = requirements;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getPlace() {
		return place;
	}

	public void setPlace(String place) {
		this.place = place;
	}

	public String getSalary() {
		return salary;
	}

	public void setSalary(String salary) {
		this.salary = salary;
	}

	public String getMailbox() {
		return mailbox;
	}

	public void setMailbox(String mailbox) {
		this.mailbox = mailbox;
	}

	public String getIntroduction() {
		return introduction;
	}

	public void setIntroduction(String introduction) {
		this.introduction = introduction;
	}

		//提供static的工具方法：根据当前 userQueryDTO 对象来组装动态查询条件
		public static Specification<Recruit> getSpecification(RecruitQueryDTO userQueryDTO)
		{
			Specification<Recruit> spec = new Specification<Recruit>() {
				public Predicate toPredicate(Root<Recruit> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
					//1.Predicate查询条件集合
					 List<Predicate> list = new ArrayList<Predicate>(); 
					 
					//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
					 if(null != userQueryDTO && !StringUtils.isEmpty(userQueryDTO.getPosition())) {
						 Predicate  p1 =  cb.like(root.get("position").as(String.class),"%"+ userQueryDTO.getPosition() + "%");
						 list.add(p1);
					 }
					 if(null != userQueryDTO && !StringUtils.isEmpty(userQueryDTO.getPlace())) {
						 Predicate  p2 =  cb.like(root.get("place").as(String.class),"%"+ userQueryDTO.getPlace() + "%");
						 list.add(p2);
					 }
					 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
					 Predicate[] p = new Predicate[list.size()];  
					 //4.CriteriaBuilder的and 函数组装 查询条件数组
					 return cb.and(list.toArray(p));  
				}
			};
			return spec;
		}
}
